package day04.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

/**
 * @Auther: Min.Liu
 * @Date: Created in 15:42 2025/5/23
 * @Description:
 */
public class aop02 {
    public static void main(String[] args) {

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(new Target());
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* foo())");
        AspectJExpressionPointcut pointcut2 = new AspectJExpressionPointcut();
        pointcut2.setExpression("execution(* bar())");
        proxyFactory.addAdvisor(new DefaultPointcutAdvisor(pointcut,(MethodInterceptor) Invocation -> {
            try {
                System.out.println("before1");
                return Invocation.proceed();
            } finally {
                System.out.println("after1");
            }
        }));
        proxyFactory.addAdvisor(new DefaultPointcutAdvisor(pointcut2,(MethodInterceptor) Invocation -> {
            try {
                System.out.println("before2");
                return Invocation.proceed();
            } finally {
                System.out.println("after2");
            }
        }));
        //proxyFactory.setProxyTargetClass(true);
        Target proxy = (Target)proxyFactory.getProxy();
        System.out.println(proxy.getClass());
        proxy.foo();
        proxy.bar();
    }

    interface jk{
        void foo();
        void bar();
    }

    static class Target implements jk{

        @Override
        public void foo() {
            System.out.println("target foo");
        }

        @Override
        public void bar() {
            System.out.println("target bar");
        }
    }
}


